import api from '../../api';
import { message } from 'antd';
import config from '../../routes/performanceAnalysis/profitOverview/dictComps';
import uuid from 'es6-uuid';

export default {
  namespace: 'profitOverviewM',

  state: {
    moduleList: [], // 模板列表
    treeData: [], // 模板设置数据
  },

  effects: {
    //  获取时间区间的可选时间范围
    *findProductTime({ payload, callback }, { call, put }) {
      const { type } = payload;
      delete payload.type;
      const res = yield call(api.profitAndLoss.findProductTime1[type], payload);
      if (res && res.code == 0) {
        callback(res);
      } else {
        callback();
        message.error(res?.msg || '获取时间区间失败！');
      }
    },
    // 获取模板列表
    *getModuleList({ payload, callback }, { call, put }) {
      const { type } = payload;
      delete payload.type;
      const res = yield call(api.profitOverview.getModuleList[type], payload);
      if (res && res.code == 0) {
        res.data &&
          res.data.map((item, index) => {
            item.checked = false;
            item.isEdit = false;
            item.randomNum = uuid(16); // 用于更新数据 标志本次和上次的请求区别 不影响业务
          });
        callback(res.data);
      } else {
        callback();
        message.error(res?.msg || '获取模板列表失败！');
      }
    },
    // 获取模板设置树形结构
    *queryTreeData({ payload, callback }, { call, put }) {
      const { type } = payload;
      delete payload.type;
      const res = yield call(api.profitOverview.queryTreeData[type], payload);
      if (res && res.code == 0) {
        // //根据key值 将树形结构的 comp加上
        const tree = _.cloneDeep(res.data || []);
        console.log('tree', tree);
        const dict = config[type];
        tree.map((item) => {
          dict.map((x) => {
            if (item.key == x.key) {
              item.component = x.component;
            } else if (item.children) {
              item.children.map((child) => {
                if (child.key == x.key) {
                  // 二级元素相同key的 加上comp
                  child.component = x.component;
                }
              });
            }
          });
        });
        callback(tree);
      } else {
        callback();
        message.error(res?.msg || '获取模板设置数据失败！');
      }
    },
    // 保存模板数据
    *saveModuleData({ payload, callback }, { call, put }) {
      const { type } = payload;
      delete payload.type;
      const res = yield call(api.profitOverview.saveModuleData[type], payload);
      if (res && res.code == 0) {
        callback();
      } else {
        message.error(res?.msg || '保存模板失败！');
      }
    },
    // 删除
    *deleteById({ payload, callback }, { call, put }) {
      const { type } = payload;
      delete payload.type;
      const res = yield call(api.profitOverview.deleteById[type], payload);
      if (res && res.code == 0) {
        callback();
      } else {
        message.error(res?.msg || '删除失败！');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
